using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using CreamX.Framework.Game;
using System.Diagnostics;
using CreamX.Framework.Game.GameEngine;

namespace CreamXStudio.XNARenderer
{
    public partial class ParticleRenderer : GraphicsDeviceControl
    {
        CreamXParticleEffect effect;
        public ParticleRenderer()
        {
            InitializeComponent();
            tmr = new Timer();
            tmr.Interval = 100;
            tmr.Enabled = true;
            tmr.Tick += new EventHandler(tmr_Tick);
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }
        Timer tmr;
        public void Setup(CreamXParticleEffect pef)
        {
            effect = pef;
            watch = new Stopwatch();
            effect.SetupEmitter();
        }
        PrimitiveBatch primitiveBatch;
        SpriteBatch sprite;
        protected override void Initialize()
        {
            primitiveBatch = new PrimitiveBatch(GraphicsDevice);
            Application.Idle += delegate { Invalidate(); };
            sprite = new SpriteBatch(GraphicsDevice);
        }
        bool init = false;
        Stopwatch watch;
        protected override void Draw()
        {
            
            GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Black);
            
            if (effect != null)
            {
                watch.Stop();
                CreamXGameEngine.Instance.Elapsed = (float)watch.Elapsed.TotalSeconds;
                Console.WriteLine(CreamXGameEngine.Instance.Elapsed);
                watch.Reset();
                watch.Start();
                sprite.Begin(SpriteBlendMode.AlphaBlend,SpriteSortMode.Immediate,SaveStateMode.None );
                
                
                effect.Update(CreamXGameEngine.Instance.Elapsed);
                effect._emitter.UseCamera = false;
                effect.Draw(sprite);
                sprite.End();
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            effect.SetPosition(e.X, e.Y);
            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
        }
    }
}
